package academic_winter2014_inse6130_project;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.apache.commons.codec.binary.Base64;
 
public class CloudGPSsecurityDecryptor extends JFrame
{
	String decryption_streetno, decryption_streetname, decryption_city, decryption_province, decryption_postalcode; 
	static String reader;
	 String key1 = "PRATIK JOSHI    "; // 128 bit key
     String key2 = "inse6130_PRATIKj"; // This key would only be shared between authorized users.


	 public static String decrypt(String key1, String key2, String encrypted) {
	        try {
	            IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

	            SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),"AES");
	           
	            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
	           
	            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
	           
	            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

	            return new String(original);
	       
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
	       
	        return null;
	    }
	 
	 
	private static final long serialVersionUID = 1L;
	
    public void start()
    {

        final JTextField decrypted_streetno = new JTextField(40);
        add(decrypted_streetno);
        final JTextField decrypted_streetname = new JTextField(40);
        add(decrypted_streetname);
        final JTextField decrypted_city = new JTextField(40);
        add(decrypted_city);
        final JTextField decrypted_province = new JTextField(40);
        add(decrypted_province);
        final JTextField decrypted_postalcode = new JTextField(40);
        add(decrypted_postalcode);
    
        JButton DECRYPT=new JButton();
        DECRYPT.setText("Decrypt");
        add(DECRYPT);
       
                DECRYPT.addActionListener(
                new ActionListener(){
                    public void actionPerformed(
                            ActionEvent e){
                    	 				   String get_streetno =  CloudGPSsecurityDecryptor.logfilereader(1);
                                           decryption_streetno = CloudGPSsecurityDecryptor.decrypt(key1,key2,get_streetno);
                                           decrypted_streetno.setText(decryption_streetno);
                                           
                                           String get_streetname =  CloudGPSsecurityDecryptor.logfilereader(2);
                                           decryption_streetname = CloudGPSsecurityDecryptor.decrypt(key1,key2,get_streetname);
                                           decrypted_streetname.setText(decryption_streetname);
                                           
                                           String get_city =  CloudGPSsecurityDecryptor.logfilereader(3);
                                           decryption_city = CloudGPSsecurityDecryptor.decrypt(key1,key2,get_city);
                                           decrypted_city.setText(decryption_city);
                                           
                                           String get_province =  CloudGPSsecurityDecryptor.logfilereader(4);
                                           decryption_province = CloudGPSsecurityDecryptor.decrypt(key1,key2,get_province);
                                           decrypted_province.setText(decryption_province);
                                           
                                           String get_postalcode =  CloudGPSsecurityDecryptor.logfilereader(5);
                                           decryption_postalcode = CloudGPSsecurityDecryptor.decrypt(key1,key2,get_postalcode);
                                           decrypted_postalcode.setText(decryption_postalcode);
                                          }
                                    }
                            );
        setLayout(new FlowLayout());
        setSize(800,800);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public static String logfilereader(int i){
       	
    	BufferedReader bufferedreader = null;
    	try {
    	if (i==1){
			String sCurrentLine = null;
 			bufferedreader = new BufferedReader(new FileReader("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt"));
				 sCurrentLine =	bufferedreader.readLine();
			 reader = sCurrentLine.substring(12);
    	}
    	if (i==2){
			String sCurrentLine = null;
 			bufferedreader = new BufferedReader(new FileReader("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt"));
			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
						 reader = sCurrentLine.substring(12);
			 System.out.println(reader);
    	}
    	if (i==3){
			String sCurrentLine = null;
 			bufferedreader = new BufferedReader(new FileReader("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt"));
			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
			 reader = sCurrentLine.substring(12);
			 System.out.println(reader);
    	}
    	if (i==4){
			String sCurrentLine = null;
 			bufferedreader = new BufferedReader(new FileReader("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt"));
 			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
 			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
			 reader = sCurrentLine.substring(12);
			 System.out.println(reader);
    	}
    	if (i==5){
			String sCurrentLine = null;
 			bufferedreader = new BufferedReader(new FileReader("C:\\Users\\Michele Toso\\Dropbox\\INSE6130_Project_Team\\Encryption.txt"));
 			 sCurrentLine =	bufferedreader.readLine();
 			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
 			 sCurrentLine =	bufferedreader.readLine();
			 sCurrentLine =	bufferedreader.readLine();
			 reader = sCurrentLine.substring(12);
			 System.out.println(reader);
    	}
    }
    	
    	catch(IOException ioe){
    		System.out.println("IO Exceptions " +ioe);
    			    	}
		catch(Exception e){
			System.out.println(" Un Known Exceptions " +e);
			}
		finally{
			try {
				if(bufferedreader!=null){
					bufferedreader.close();
				}
			}
			catch (IOException e) {

				e.printStackTrace();
			}
		}
    	return reader;
    }
 
    public static void main(String args[])
    {
        new CloudGPSsecurityDecryptor().start();
    }
}
